package com.fduss.artStore.service.impl;

import com.fduss.artStore.dao.*;
import com.fduss.artStore.dao.impl.*;
import com.fduss.artStore.entity.*;
import com.fduss.artStore.service.OrderService;

import java.sql.Timestamp;
import java.util.List;

/**
 * Created by alex on 29/06/2017.
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao;
    private OrderDetailDao orderDetailDao;
    private TypesFrameDao frameDao;
    private TypesGlassDao glassDao;
    private TypesMattDao mattDao;
    private TypesShipperDao shipperDao;
    private TypesStatusCodeDao statusCodeDao;

    public OrderServiceImpl() {
        orderDao = new OrderDaoImpl();
        orderDetailDao = new OrderDetailDaoImpl();
        frameDao = new TypesFrameDaoImpl();
        glassDao = new TypesGlassDaoImpl();
        mattDao = new TypesMattDaoImpl();
        shipperDao = new TypesShipperDaoImpl();
        statusCodeDao = new TypesStatusCodeDaoImpl();
    }

    @Override
    public Order createOrder(int artworkID, int customerId, int frameId, int mattId, int glassId) {
        Order order = new Order();
        order.setCustomerId(customerId);
        order.setDateCreated(new Timestamp(System.currentTimeMillis()));
        Order newOrder = orderDao.add(order);

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(newOrder.getOrderId());
        orderDetail.setArtworkID(artworkID);
        orderDetail.setFrameID(frameId);
        orderDetail.setMattID(mattId);
        orderDetail.setGlassID(glassId);
        orderDetailDao.add(orderDetail);
        return newOrder;
    }

    @Override
    public Order completeOrder(int orderId) {
        Order order = orderDao.getOne(orderId);
        order.setDateCompleted(new Timestamp(System.currentTimeMillis()));
        orderDao.update(order);
        return order;
    }

    @Override
    public OrderDetail updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailDao.update(orderDetail);
    }

    @Override
    public void deleteOrder(int orderId) {
        orderDao.delete(orderId);
        orderDetailDao.deleteByOrder(orderId);
    }

    @Override
    public OrderDetail getOrderDetail(int orderId) {
        OrderDetail orderDetail = orderDetailDao.getByOrder(orderId);
        if (orderDetail == null)
            return null;
        orderDetail.setMatt(mattDao.getOne(orderDetail.getMattID()));
        orderDetail.setFrame(frameDao.getOne(orderDetail.getFrameID()));
        orderDetail.setGlass(glassDao.getOne(orderDetail.getGlassID()));
        return orderDetail;
    }

    @Override
    public List<Order> getOrders(int customerId) {
        return orderDao.getByCustomer(customerId);
    }

    @Override
    public List<Order> getNotCompletedOrders(int customerId) {
        return orderDao.getByStateAndCustomer(customerId, 0);
    }

    @Override
    public List<Order> getCompletedOrders(int customerId) {
        return orderDao.getByStateAndCustomer(customerId, 1);
    }

    @Override
    public List<Order> getAllOrders() {
        return orderDao.getAll();
    }

    @Override
    public List<TypesFrame> getAllFrames() {
        return frameDao.getAll();
    }

    @Override
    public List<TypesGlass> getAllGlasses() {
        return glassDao.getAll();
    }

    @Override
    public List<TypesMatt> getAllMatts() {
        return mattDao.getAll();
    }

    @Override
    public List<TypesShipper> getAllShippers() {
        return shipperDao.getAll();
    }
}
